package com.shadow.demo.shiro.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.shadow.demo.common.constants.PunctuationConstants;
import com.shadow.demo.common.enums.shiro.PCodeEnum;
import com.shadow.demo.data.shiro.entity.User;
import com.shadow.demo.data.bo.Query;
import com.shadow.demo.data.shiro.mapper.UserMapper;
import com.shadow.demo.shiro.util.UserUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Map;

/**
 * Created by shadow
 * on 2022-6-29.
 */
@Service
public class UserService extends BaseService<UserMapper, User> implements IService<User> {

    @Resource
    private UserMapper userMapper;

    /**
     * 通过账户获取用户信息
     *
     * @param account 账户
     * @return {@link User}
     * */
    public User getUserByAccount(String account) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<User>()
                .eq(User::getAccount, account);
        return userMapper.selectOne(wrapper);
    }

    /**
     * 验证存在性
     *
     * @param account 账户
     * @return 是否存在 false 不存在 true 存在
     * */
    public boolean verifyExist(final String account) {
        if (StringUtils.isNotBlank(account)) {
            final LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<User>()
                    .eq(User::getAccount, account);
            return userMapper.selectCount(wrapper) > 0L;
        }
        throw new IllegalArgumentException("账户为空");
    }

    /**
     * 验证是否可以编辑
     *
     * @param id 主键
     * @return 是否可以编辑 false 不 true 可
     * */
    public boolean verifyEditable(final String id) {
        if (StringUtils.isNotBlank(id)) {
            final LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<User>()
                    .eq(User::getId, id)
                    .eq(User::getStatus, 1);
            return userMapper.selectCount(wrapper) == 1L;
        }
        throw new IllegalArgumentException("不存在用户");
    }

    /**
     * 分页获取用户列表
     *
     * @param params 查询参数
     * @return {@link User}
     * */
    public Page<User> getList(final Map<String, Object> params) {
        final String name = (String) params.get("name");
        // 查询删除数据的权限
        boolean deletePermissions = UserUtils.hasPermissions(PCodeEnum.USER_DEL_SELECT.getCode());
        final LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<User>()
                .eq(!deletePermissions, User::getStatus, 1)
                .like(StringUtils.isNotBlank(name), User::getName, name)
                .orderByDesc(User::getName);
        return userMapper.selectPage(new Query<User>(params).getPage(), wrapper);
    }

    /**
     * 逻辑删除
     * @param ids 以逗号分割的id
     * */
    public int delete(final String ids) {
        String[] split = ids.split(PunctuationConstants.SPLIT_COMMA);
        if (ArrayUtils.isEmpty(split)) {
            return 0;
        }
        LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<User>()
                .set(User::getStatus, "0")
                .in(User::getId, Arrays.asList(split));
        return userMapper.update(wrapper);
    }
}
